package nohttp;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.yolanda.nohttp.rest.Response;
import com.com.ruanmeng.utils.JsonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.example.lenovo.weiyi.BaseActivity;
import com.example.lenovo.weiyi.LoginActivity;
import org.json.JSONException;
import org.json.JSONObject;


/**
 * Created by 小卷毛 on 2016/4/28 0028.
 */
public abstract class CustomHttpListener implements HttpListener<String> {

    private JSONObject object;
    private Context context;
    private boolean isGson;
    private Class<?> dataM;

    public CustomHttpListener(Context context, boolean isGson, Class<?> dataM) {
        this.context = context;
        this.isGson = isGson;
        this.dataM = dataM;
    }

    @Override
    public void onSucceed(int what, Response<String> response) {
        Log.i("onSucceed", "请求成功：\n" + response.get());
        try {
            object = new JSONObject(response.get());
            if ("100".equals(object.getString("msgcode"))) {  //100是成功的情况
                if (isGson && dataM != null&&JsonUtil.jsonToBean(response.get(),dataM)!=null) {
                    doWork(JsonUtil.jsonToBean(response.get(),dataM), true);
                    Log.i("isGo", "true");
                }
            } else if ("102".equals(object.getString("msgcode"))) {//token失效
                try {
                    setcleardata();
                    BaseActivity.clearActivity();
                    BaseActivity.clearMainActivity();
                    Intent in = new Intent(context, LoginActivity.class);
                    context.startActivity(in);
                } catch (Exception o) {
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            try {
                onFinally(object);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    public abstract void doWork(Object data, boolean isOne);

    public void onFinally(JSONObject obj) throws JSONException {
    }

    ; //解析完成，如要执行操作，可重写该方法。

    @Override
    public void onFailed(int what, String url, Object tag, Exception e, int responseCode, long networkMillis) {
        Log.i("onFailed", "请求失败：\n" + e.getMessage());
//        CommonUtil.showToask(context, "请求数据失败");
    }

    public void setcleardata() {
        PreferencesUtils.putString(context, "user_name", "");
//        PreferencesUtils.putString(context, "phone", "");
        PreferencesUtils.putString(context, "psw", "");
        PreferencesUtils.putString(context, "enable", "");
        PreferencesUtils.putString(context, "salt", "");
        PreferencesUtils.putString(context, "danwei", "");
        PreferencesUtils.putString(context, "bumen", "");
        PreferencesUtils.putString(context, "email", "");
        PreferencesUtils.putString(context, "statues", "");
        PreferencesUtils.putString(context, "url", "");
        PreferencesUtils.putString(context, "uid", "");
        PreferencesUtils.putString(context, "age", "");
        PreferencesUtils.putString(context, "statues", "");
        PreferencesUtils.putString(context, "IsQD", "");
        PreferencesUtils.putString(context, "qcard", "");
        PreferencesUtils.putInt(context, "Login", 0);//登陆标记
    }
}
